package com.eblacorp.archive.security.ldap;

import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;

import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DistinguishedName;

import com.eblacorp.archive.security.ldap.model.Group;
/**
 * @author Mahmoud.Ezzat
 * 
 */
public class GroupContextMapper implements ContextMapper<Group> {

	public Group mapFromContext(Object ctx) {
		DirContextOperations dirContext = (DirContextOperations) ctx;
		DistinguishedName dn = new DistinguishedName(dirContext.getDn());
		String fullDn;
		fullDn = dirContext.getNameInNamespace();
		
		Group group = new Group();
		group.setDn(fullDn);
		group.setName(dirContext.getStringAttribute("cn"));
		String[] membersArray = dirContext.getStringAttributes("uniqueMember");
		if (membersArray != null) {
			List list = Arrays.asList(membersArray);
			group.setMembers(new TreeSet(list));
		}
		return group;
	}
}
